/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Facebook;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.CreateException;
import javax.ejb.Stateful;
import javax.ws.rs.core.MediaType;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import sun.security.krb5.internal.KDCOptions;

/**
 *
 * @author andre
 */
@Stateful
public class FacebookClient implements FacebookClientLocal {
    private static final String GRAPH_API = "https://graph.facebook.com";
    
    private static final String FRIENDS = "friends";
    
    private static final String ACCESS_TOKEN = "access_token";
    
    
    private String Code;
    private String UId;
    private String Nome;

    public FacebookClient() {
    }
       
    @Override
    public String getCode() {
        return this.Code;
    }
    
    @Override
    public void setCode(String code) {
        this.Code = code;
    }

    @Override
    public FacebookClientLocal create() throws CreateException {
        return new FacebookClient();
    }

    public void ejbCreate() throws CreateException {
        
    }

    @Override
     public String getUId() {
        return this.UId;
    }
    
    @Override
    public void setUId(String uid) {
        this.UId = uid;
    }

    @Override
    public void setOAuthInfo(String code, String user_id) {
        this.Code = code;
        this.UId = user_id;
    }

    @Override
    public String getUserInfoJson() {
        if(this.UId != null && !this.UId.isEmpty()){
            ClientConfig config = new DefaultClientConfig();
            Client client = Client.create(config);
            WebResource service = client.resource(FacebookClient.GRAPH_API);
            service = service.path(this.UId);
            String json = service.accept(MediaType.APPLICATION_JSON).get(String.class);
            return json;
        }
        return null;
    }

    @Override
    public String getUserFriendsJson() {
        if(this.UId != null && !this.UId.isEmpty() && this.Code != null && !this.Code.isEmpty()){
            ClientConfig config = new DefaultClientConfig();
            Client client = Client.create(config);
            WebResource service = client.resource(FacebookClient.GRAPH_API);
            service = service.path(this.UId).path(FacebookClient.FRIENDS);
            service = service.queryParam(FacebookClient.ACCESS_TOKEN, this.Code);
            System.out.println(service.toString());
            String json = service.accept(MediaType.APPLICATION_JSON).get(String.class);
            return json;
        }
        return null;
    }
    
    @Override
    public Map<String, String> getUserFriendsUIds(){
        try {
            HashMap<String, String> uids = new HashMap<String, String>();
            String json = this.getUserFriendsJson();
            JSONObject jo = new JSONObject(json);
            JSONArray ja = jo.getJSONArray("data");
            for(int i = 0; i < ja.length(); i++){
                JSONObject f = ja.getJSONObject(i);
                uids.put(f.getString("id"), f.getString("name"));
            }
            return uids;
        } catch (JSONException ex) {
            return new HashMap<String, String>();
        }
    }

    @Override
    public String getNome() {
        return this.Nome;
    }

    @Override
    public void setNome(String nome) {
        this.Nome = nome;
    }

    @Override
    public void setUserInfo() {
        try {
            JSONObject jo = new JSONObject(this.getUserInfoJson());
            this.setNome(jo.getString("name"));
        } catch (JSONException ex) {
            Logger.getLogger(FacebookClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
